define(function(require, exports, module) {

	"use strict";
	require("mui");
	require("jquery");
	require("../newOrder/dateInput.js");
	var muiTips = require("muiTips"); //封装的弹框
	var abpdata = require("abpinit");
	var abp = abpdata.abpstart;
	//钉钉配置文件
	require('http://g.alicdn.com/ilw/ding/0.8.9/scripts/dingtalk.js');
	var dingTalkNav = require('dd_config');
	dingTalkNav.RightNavBar(false, "更多", "default"); //钉钉右侧按钮隐藏

	$(function() {
		//钉钉返回清除缓存
		if(sessionStorage.getItem("productUpdate")) {
			//ios
			dingTalkNav.LeftNavBar(clearSessionDDUpdate);
			//ad
			dingTalkNav.leftAndroid(clearSessionDDUpdate);
		}

		//扫码
		mui("body").on("tap", "#scan-icon", function() {
			dingTalkNav.ScanCode(ScanCode);
		});

		function ScanCode(data) {
			$("#product-bar").val(data.text);
		}

		//上传图片
		mui("body").on("tap", ".add-new-img", function() {
			dingTalkNav.uploadImage(uploadIMG);
		});
		//   
		function uploadIMG(result) {
			var imgSRC;
			var srcList = result;
			//			$("body").append(JSON.stringify(result));
			for(var i = 0; i < srcList.length; i++) {
				imgSRC = srcList[i];
				$(".add-new-img").before('<img class="goodsIMG" src="' + imgSRC + '" alt="" />');
			}
		}

		//图片点击浏览
		mui("body").on("tap", ".goodsIMG", function() {
			var urls = [];
			var img = $(".goodsIMG");
			var imgURL;
			for(var i = 0; i < img.length; i++) {
				imgURL = $(".goodsIMG").eq(i).attr("src");
				urls.push(imgURL);
			}
			var current = $(this).attr("src");
			dingTalkNav.previewImage(urls, current);
		});

		//钉钉返回清除缓存
		function clearSessionDDUpdate() {
			sessionStorage.removeItem("productUpdate");
			document.location.href = "../../src/stock/stockDetail.html?dd_nav_bgcolor=FFFF943E";
		}

		function clearSessionUpdate() {
			sessionStorage.removeItem("productUpdate");
		}
		mui("body").on("tap", "#back", function() {
			clearSessionUpdate();
		});

		goodsInfo();
		//页面初始化渲染数据
		function goodsInfo() {
			if(sessionStorage.getItem("data-stockQuery")) {

				var stockQuery = sessionStorage.getItem('data-stockQuery');
				var goodsInfo = JSON.parse(stockQuery);
				console.log(goodsInfo);
				if(sessionStorage.newInfoForGoods) {
					var newInfoForGoods = JSON.parse(sessionStorage.newInfoForGoods);

					console.log(newInfoForGoods);
					$("#goods-num").text(newInfoForGoods.goodsSerial); //货品编号
					$("#goods-name").val(newInfoForGoods.goodsName); //货品名字
					$("#goods-name").attr("data-id", newInfoForGoods.id); //货品ID
					$("#goods-type").text(sessionStorage.sortName); //货品分类名称
					$("#goods-type").attr("data-id", (sessionStorage.sortCatalogId)); //货品分类ID
					$("#goods-rule").val(newInfoForGoods.specValue); //规格值
					$("#goods-unit").val(newInfoForGoods.unit); //单位
					$("#goods-cost").val(newInfoForGoods.costPrice); //成本
					$("#goods-price").val(newInfoForGoods.buyMaxPrice); //进价
					$("#goods-bid").val(newInfoForGoods.sellMinPrice); //售价
					$("#product-bar").val(newInfoForGoods.barCode); //条形码
					$("#notes").val(newInfoForGoods.notes); //备注

				} else {
					$("#goods-num").text(goodsInfo.goodsSerial); //货品编号
					$("#goods-name").val(goodsInfo.goodsName); //货品名字
					$("#goods-name").attr("data-id", goodsInfo.id); //货品ID
					$("#goods-type").text(goodsInfo.infoGoodsCatalog.catalogName); //货品分类名称
					$("#goods-type").attr("data-id", (goodsInfo.catalogId)); //货品分类ID
					$("#goods-rule").val(goodsInfo.specValue); //规格值
					$("#goods-unit").val(goodsInfo.unit); //单位
					$("#goods-cost").val(goodsInfo.costPrice); //成本
					$("#goods-price").val(goodsInfo.buyMaxPrice); //进价
					$("#goods-bid").val(goodsInfo.sellMinPrice); //售价
					$("#product-bar").val(goodsInfo.barCode); //条形码
					$("#notes").val(goodsInfo.notes); //备注

					var goodsImg = goodsInfo.infoGoodsImg;
					for(var i = 0, j = goodsImg.length; i < j; i++) {
						var imgSRC = goodsImg[i].imgPath;
						$(".add-new-img").before('<img class="goodsIMG" src="' + imgSRC + '" alt="" />');
					}
				}

			}
		}

		function alertTshow(i) { //输入框获取焦点 提示窗隐藏
			$(".alertT").eq(i).stop().animate({
				"top": "70px"
			}, 400, function() {
				$(this).delay(1000).fadeOut(500, function() {
					$(this).css("top", "-55px");
					$(this).show();
				});
			});
		}

		function ADDinvalid(i) {
			$(".testI").eq(i).addClass("invalid");
			$(".testI").eq(i).attr("placeholder", "不能为空");
		}

		function REMOVEinvalid(i) {
			$(".testI").eq(i).removeClass("invalid");
		}

		function changeGoods() {
			var goodsSerial = $("#goods-num").text(); //货品编号
			var goodsName = $("#goods-name").val(); //货品名字
			var goodsID = $("#goods-name").data("id"); //货品id
			var sortName = $("#goods-type").text(); //货品分类名称
			var sortCatalogId = $("#goods-type").attr("data-id"); //货品分类ID
			var specValue = $("#goods-rule").val(); //规格值
			var goodsRule = $("#goods-rule").val(); //规格值
			var unit = $("#goods-unit").val(); //单位值
			var costPrice = $("#goods-cost").val(); //成本
			var sellMinPrice = $("#goods-price").val(); //售价
			var buyMaxPrice = $("#goods-bid").val(); //进价
			var barCode = $("#product-bar").val(); //条形码	
			var notes = $("#notes").val(); //备注

			//获取图片信息
			var infoGoodsImg = [];
			var imgs = $(".goodsIMG");

			for(var i = 0; i < imgs.length; i++) {
				var imgSrc = $(".goodsIMG").eq(i).attr("src");
				var imgSrcObj = {
					"imgPath": imgSrc, //(string): 图片路径,
					"imgTitle": imgSrc, //(string): 图片名,
					"id": 0 //(integer, optional): Id
				};
				infoGoodsImg.push(imgSrcObj);
			}
			//			if(!goodsSerial) {
			//				ADDinvalid(0);
			//			} else {
			//				REMOVEinvalid(0);
			//			}
			if(!goodsName) {
				ADDinvalid(0);
			} else {
				REMOVEinvalid(0);
			}
			if(!sortCatalogId) {
				$("#goods-type").addClass("red");
			} else {
				$("#goods-type").removeClass("red");
			}
			if(!goodsRule) {
				ADDinvalid(1);
			} else {
				REMOVEinvalid(1);
			}
			if(!unit) {
				ADDinvalid(2);
			} else {
				REMOVEinvalid(2);
			}

			if(!goodsSerial) {
				alertTshow(0);
				return;
			}
			if(!goodsName) {
				alertTshow(1);
				return;
			}
			if(!sortCatalogId) {
				alertTshow(2);
				return;
			}
			if(!goodsRule) {
				alertTshow(3);
				return;
			}
			if(!unit) {
				alertTshow(4);
				return;
			}

			var option = {};
			var dataModel = {
				"catalogId": sortCatalogId, //0分类 ,
				"goodsSerial": goodsSerial, //str货品编号 ,
				"goodsName": goodsName, //str货品名称 ,
				"barCode": barCode, //str条形码 ,
				"unit": unit, //str
				"whId": 0, //0仓库id ,
				"notes": notes, //str备注 ,
				"goodsType": sortCatalogId, //0
				"costPrice": costPrice, //0参考成本 ,
				"buyMaxPrice": buyMaxPrice, //0最大采购价 ,
				"sellMinPrice": sellMinPrice, //0最低销售价 ,
				"specValue": specValue, //(string, optional): 规格 ,
				"buyCycle": 0, //(number, optional): 采购周期 ,
				"dailyProduceNumber": 0, //(number, optional): 日产量 ,
				"infoGoodsImg": infoGoodsImg,
				"infoGoodsSpecValue": [],
				"id": goodsID //0
			};
			console.log(dataModel);
			abp.services.app.infoGoods.update(dataModel, option).
			done(function(data) {

				muiTips.alertAutoma(' ', "修改成功", 2, openHref);

			}).fail(function(data) {

				muiTips.alertAutoma(' ', data.message, 2, "default");

			});
		}

		function openHref() {
			document.location.href = "../../src/chioceProduct/chioceProductList.html?dd_nav_bgcolor=FFFF943E";
		}

		mui("body").on("tap", "#sortCatalogId", function() {

			var goodsSerial = $("#goods-num").text(); //货品编号
			var goodsName = $("#goods-name").val(); //货品名字
			var goodsID = $("#goods-name").data("id"); //货品id
			var specValue = $("#goods-rule").val(); //规格值
			var unit = $("#goods-unit").val(); //单位值
			var costPrice = $("#goods-cost").val(); //成本
			var sellMinPrice = $("#goods-price").val(); //售价
			var buyMaxPrice = $("#goods-bid").val(); //进价
			var barCode = $("#product-bar").val(); //条形码	
			var notes = $("#notes").val(); //备注

			var newInfoForGoods = {
				"goodsSerial": goodsSerial, //货品编号
				"goodsName": goodsName, //货品名字
				"id": goodsID,
				"specValue": specValue, //规格值
				"unit": unit, //单位值
				"costPrice": costPrice, //成本
				"buyMaxPrice": buyMaxPrice, //进价
				"sellMinPrice": sellMinPrice, //售价
				"barCode": barCode, //条形码	
				"notes": notes //备注
			};
			sessionStorage.newInfoForGoods = JSON.stringify(newInfoForGoods);
			document.location.href = "../chioceProduct/chioceSort.html";
		});

		mui("body").on("tap", "#change-btn", function() {
			changeGoods();
		});

	});

});